package com.bitcola.exchange.rest;

import com.bitcola.exchange.biz.ColaExchangeBiz;
import com.bitcola.exchange.data.RushParams;
import com.bitcola.exchange.service.MatchService;
import com.bitcola.security.common.msg.AppResponse;
import com.bitcola.exchange.service.RushService;
import com.fasterxml.jackson.databind.node.BigIntegerNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 *  @author wx
 * @create 2019-04-22 14:38
 **/
@RestController
@RequestMapping("statistic")
public class ColaRushController {
    @Autowired
    RushService rushService;

    @Resource
    MatchService matchService;
    @Autowired
    ColaExchangeBiz colaExchangeBiz;
    @RequestMapping(method = RequestMethod.GET)
    public void statistic(HttpServletResponse response) throws IOException {
        String sTitle = "uid,可用,冻结,总和";


        List<Map<String,Object>>result=colaExchangeBiz.statisticHx();

        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(sTitle).append("\r\n");

        for(Map<String,Object>objectMap:result){

            stringBuilder.append(objectMap.get("user_id").toString()).append(",").append(new BigDecimal(objectMap.get("balance_available").toString()).toPlainString()).append(",").append(new BigDecimal(objectMap.get("balance_frozen").toString()).toPlainString()).append(",").append(new BigDecimal(objectMap.get("sum").toString()).toPlainString()).append("\r\n");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fn = "hx持币_" + sdf.format(new Date()).toString() + ".csv";
        // 读取字符编码
        String utf = "UTF-8";

        // 设置响应
        response.setContentType("application/ms-txt.numberformat:@");
        response.setCharacterEncoding(utf);
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "max-age=30");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fn, utf));
        var os=response.getOutputStream();
        os.write(stringBuilder.toString().getBytes("GBK"));
        os.flush();
    }


    public long reloadDepth(String pair){

        long ll= matchService.initDepth(pair);
        return ll;
    }



}
